package com.we.risk.phoneBook.common;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Utils {
    public static String forMattedPhoneBookNumberDePrecated(String phone) {
        if (phone != null) {
            phone = phone.replaceAll("[^\\d]", "");
            String mobRegex = "1[3-9][0-9]{9}$";
            String mobPart = getRegexResStr(phone, mobRegex);
            if (mobPart != null && mobPart.length() > 0) {
                return mobPart;
            } else {
                String fixRegex = "^(0[0-9]{2,3})?-?[2-9][0-9]{6,7}";
                String fixPart = getRegexResStr("0" + phone, fixRegex);
                if (fixPart != null && fixPart.length() > 0) {
                    return fixPart;
                } else {
                    String ad400Regex = "^(400[0-9]{6,8})|(800[0-9]{6,8})";
                    String ad400Part = getRegexResStr(phone, ad400Regex);
                    if (ad400Part != null && ad400Part.length() > 0) {
                        return ad400Part;
                    } else {
                        String b9596Regex = "(95|96)[0-9]{3,4}";
                        String b9596Part = getRegexResStr(phone, b9596Regex);
                        if (b9596Part != null && b9596Part.length() > 0) {
                            return b9596Part;
                        } else {
                            String specRegex =
                                    "^(110|119|120|122|999|112|114|12395|12121|12117|12119|12110|10000|10001|10010|10086|10050|12306|118114|116114)$";
                            String specPart = getRegexResStr(phone, specRegex);
                            if (specPart != null && specPart.length() > 0) {
                                return specPart;
                            }
                        }
                    }
                }
            }
        }
        return null;
    }

    public static String forMattedPhoneBookNumber(String phone) {
        phone = (phone == null ? "" : phone);
        phone = phone.replaceAll("[^\\d]", "");
        return phone;
    }

    private static String getRegexResStr(String input, String pattern) {
        Pattern p = Pattern.compile(pattern);
        Matcher matcher = p.matcher(input);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }

    public static boolean isRegBokPhn(String phone) {
        if (phone != null) {
            String mobRegex = "1[3-9][0-9]{9}$";
            String mobPart = getRegexResStr(phone, mobRegex);
            if (mobPart != null) {
                return (mobPart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean isTel(String phone) {
        if (phone != null) {
            String mobRegex = "^(0[0-9]{2,3})?-?[2-9][0-9]{6,7}";
            String mobPart = getRegexResStr(phone, mobRegex);
            if (mobPart != null) {
                return (mobPart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean is400(String phone) {
        if (phone != null) {
            String mobRegex = "^(400[0-9]{6,8})|(800[0-9]{6,8})";
            String mobPart = getRegexResStr(phone, mobRegex);
            if (mobPart != null) {
                return (mobPart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean is9596(String phone) {
        if (phone != null) {
            String mobRegex = "(95|96)[0-9]{3,4}";
            String mobPart = getRegexResStr(phone, mobRegex);
            if (mobPart != null) {
                return (mobPart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean isSpc(String phone) {
        if (phone != null) {
            String mobRegex =
                    "^(110|119|120|122|999|112|114|12395|12121|12117|12119|12110|10000|10001|10010|10086|10050|12306|118114|116114)$";
            String mobPart = getRegexResStr(phone, mobRegex);
            if (mobPart != null) {
                return (mobPart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean hasFmyTag(String name) {
        if (name != null) {
            String regex =
                    "父母|爸|妈|爹|老娘|皇额娘|爷爷|奶奶|老公|公公|老公|老婆|老婆大人|婆娘|外婆|婆婆|丈夫|媳妇|妻子|儿子|女儿|dad|mom|姐|妹|伯";
            String namePart = getRegexResStr(name.toLowerCase(), regex);
            if (namePart != null) {
                return (namePart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean hasLonTag(String name) {
        if (name != null) {
            String regex = "办|贷|申请|借|中介|网贷|骗|套现|现金|代刷|口子|钱庄|地下|讨|债|催|提额|刷卡";
            String namePart = getRegexResStr(name, regex);
            if (namePart != null) {
                return (namePart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean hasGood1Tag(String name) {
        if (name != null) {
            String regex = "科室|实习|党支部|党办|办公室|接机|值班";
            String namePart = getRegexResStr(name, regex);
            if (namePart != null) {
                return (namePart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean hasGood2Tag(String name) {
        if (name != null) {
            String regex =
                    "监理|消防|地板|总监|集团|打印|主席|电梯|轮胎|卡号|打印机|姥姥|永利|防|姥爷|股长|中行|国税|娣|宝马|参谋|支书|客户经理|中央|办证|媳妇儿|班车|别克|银行卡|农村信用|管理局|劳动局";
            String namePart = getRegexResStr(name, regex);
            if (namePart != null) {
                return (namePart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static boolean hasBadTag(String name) {
        if (name != null) {
            String regex =
                    "专线电话|老爷|山东|电费|兴业银行|金融|家政|中信银行|大华|合肥|组长|产中|国人|门口|温州|民政局|服装|小龙虾|美容院|驾照|对面|买车|行车|报警|刷卡|日本|公证|骚扰|资源|法律|刷卡机|人力|警察|王八蛋|分|房管|社会|车子|死|人事部|行政|工程部|联合|名片|交电|伙子|小伙子|信报|贴膜|商机|微商|粉丝";
            String namePart = getRegexResStr(name, regex);
            if (namePart != null) {
                return (namePart.length() > 0);
            }
            return false;
        }
        return false;
    }

    public static Integer hiveUdfLength(String s) {
        if (s == null) {
            return null;
        }
        byte[] data = s.getBytes();
        int len = 0;
        for (int i = 0; i < data.length; i++) {
            if (isUtfStartByte(data[i])) {
                len++;
            }
        }
        return len;
    }

    private static boolean isUtfStartByte(byte b) {
        return (b & 0xC0) != 0x80;
    }

    public static void main(String[] args) {
        String input = "…\uD83D\uDE07";
        System.out.println("hiveUdfLen:" + hiveUdfLength(input));
        System.out.println("java len: " + input.length());
    }
}
